/// Author: QP
/// Version: 1.0
/// Date:2022/11
/// HCC转换FHIR业务操作，将HCC.FHIR.Transformer封装为业务操作
Class HCC.FHIR.TransformOperation Extends Ens.BusinessOperation
{

Parameter INVOCATION = "Queue";

Parameter SETTINGS = "Debug,DebugFilePath";

/// 是否调试模式。调试模式下会将中间产生的SDA和FHIR输出到DebugFilePath目录下
Property Debug As %Boolean [ InitialExpression = 0 ];

/// 调试模式下，中间文件产生路径
Property DebugFilePath As %String;

/// 转换器
Property Transformer As HCC.FHIR.Transformer [ Internal ];

/// 流转换为FHIR
Method StreamToFHIR(pRequest As Ens.StreamContainer, Output pResponse As Ens.StreamContainer) As %Status
{
	Set tSC = $$$OK
	Set pResponse = ##class(Ens.StreamContainer).%New()
	Set tOutput=##class(%Stream.GlobalCharacter).%New()
	Set tSC = ..Transformer.HCCToFHIR(pRequest.Stream, .tOutput)
	Quit:($$$ISERR(tSC)) tSC
	Set tSC = pResponse.StreamSet(tOutput)
	Quit tSC
}

/// 文件转换为FHIR
Method FileToFHIR(pRequest As Ens.StringContainer, Output pResponse As Ens.StreamContainer) As %Status
{
	Set pResponse = ##class(Ens.StreamContainer).%New()	
	Set tSource=##class(%Stream.FileBinary).%New()
	Set tSC=tSource.LinkToFile(pRequest.StringValue)
	Quit:($$$ISERR(tSC)) tSC
	Do tSource.Rewind()
	Set pResponse = ##class(Ens.StreamContainer).%New()
	Set tSC = ..Transformer.HCCToFHIR(tSource, .tOutput)
	Quit:($$$ISERR(tSC)) tSC
	Do pResponse.Stream.CopyFrom(tOutput)
	Quit $$$OK
}

XData MessageMap
{
<MapItems>
	<MapItem MessageType="Ens.StreamContainer"> 
		<Method>StreamToFHIR</Method>
	</MapItem>
	<MapItem MessageType="Ens.StringContainer"> 
		<Method>FileToFHIR</Method>
	</MapItem>
</MapItems>
}

/// This user callback method is called via initConfig() from %OnNew() or in the case of SOAP Services from OnPreSOAP()
Method OnInit() As %Status
{
	Set ..Transformer=##class(HCC.FHIR.Transformer).%New(..Debug,..DebugFilePath)
	Quit $$$OK
}

}
